#include <all_wlib.h>
#pragma hdrstop
//#pragma package(smart_init)
#pragma resource "*.dfm"

#include "ozf_PlaceEdit.h"

bool EditPlacePos( PHCoord p )
  {  HAutoPtr<TPlaceEditForm> d( new TPlaceEditForm(NULL) );

     d->SetCoord( p );
     if ( d->ShowModal() != mrOk )
       return false;

     *p = d->Coord;
 return true;
}

//---------------------------------------------------------------------------
__fastcall TPlaceEditForm::TPlaceEditForm(TComponent* Owner)
    : TForm(Owner)
  {
}

void TPlaceEditForm::SetCoord( PHCoord p )
  {
     Coord = *p;
     pages->ActivePageIndex = GOPT( "CoordEdit/Page", 0 );
}
//---------------------------------------------------------------------------
void __fastcall TPlaceEditForm::idSetCoord(TObject *Sender)
  {  double d, m, s;

     //Coord -> fields
     if ( ((TTabSheet*)Sender)->PageIndex == 0 ) {
       Val2DMS( Coord.Lat, d, m, s );
       edtDMS_DN->Text = Message( "%.0f", d );
       edtDMS_MN->Text = Message( "%.0f", m );
       edtDMS_SN->Text = Message( "%.3f", s );

       Val2DMS( Coord.Lon, d, m, s );
       edtDMS_DE->Text = Message( "%.0f", d );
       edtDMS_ME->Text = Message( "%.0f", m );
       edtDMS_SE->Text = Message( "%.3f", s );
     } else
     if ( ((TTabSheet*)Sender)->PageIndex == 1 ) {
       Val2DM( Coord.Lat, d, m );
       edtDM_DN->Text = Message( "%.0f", d );
       edtDM_MN->Text = Message( "%.3f", m );
       Val2DM( Coord.Lon, d, m );
       edtDM_DE->Text = Message( "%.0f", d );
       edtDM_ME->Text = Message( "%.3f", m );
     } else
     if ( ((TTabSheet*)Sender)->PageIndex == 2 ) {
       edtD_DN->Text = Message( "%.5f", Coord.Lat );
       edtD_DE->Text = Message( "%.5f", Coord.Lon );
     } else
     if ( ((TTabSheet*)Sender)->PageIndex == 3 ) {
       edtUTM_N->Text = Coord.y;
       edtUTM_E->Text = Coord.x;
     } else
       ;
}
//---------------------------------------------------------------------------
void __fastcall TPlaceEditForm::btnOkClick(TObject *Sender)
  {  HCoord p;

     //Fields -> Coord
     if ( pages->ActivePageIndex == 0 ) {
       if ( !DMS2Val( atof( edtDMS_DN->Text.c_str() ),
                      atof( edtDMS_MN->Text.c_str() ),
                      atof( edtDMS_SN->Text.c_str() ),
                      p.Lat ) ||
            !DMS2Val( atof( edtDMS_DE->Text.c_str() ),
                      atof( edtDMS_ME->Text.c_str() ),
                      atof( edtDMS_SE->Text.c_str() ),
                      p.Lon ) ) {
         AskMessage( Handle, NULL, "Error coordinates values" );
         return;
       }
     } else
     if ( pages->ActivePageIndex == 1 ) {
       if ( !DM2Val( atof( edtDM_DN->Text.c_str() ),
                     atof( edtDM_MN->Text.c_str() ),
                     p.Lat ) ||
            !DM2Val( atof( edtDM_DE->Text.c_str() ),
                     atof( edtDM_ME->Text.c_str() ),
                     p.Lon ) ) {
         AskMessage( Handle, NULL, "Error coordinates values" );
         return;
       }
     } else
     if ( pages->ActivePageIndex == 2 ) {
       p.Lat = atof( edtD_DN->Text.c_str() );
       p.Lon = atof( edtD_DE->Text.c_str() );
     } else
     if ( pages->ActivePageIndex == 3 ) {
       return;
     } else
       ;

     SOPT( "CoordEdit/Page", pages->ActivePageIndex );
     ozi2UTM( p.Lat, p.Lon, p.x, p.y );
     Coord = p;
     ModalResult = mrOk;
}
//---------------------------------------------------------------------------

